/**
 * 数据中心
 * @author yangjian
 */
define(function(require, exports) {

	//加载依赖
	require("ajaxproxy");
	var common = require("common");
    var template = require("jtemplate");
    window.select_num = 1;
	exports.init = function(configs) {

	    //获取品名厂牌号
		$(".ajaxTab").on('click',function () {

			var tab = $(this).data('tab');
			$('[name=tab]').val(tab);
			$('#search-form').submit();
        });

        //添加数据
        $("#content-add").on("click", function() {
            common.post("行情数据", "content-add-template", {width:850, top:200}, {status:'1', index_show: 1}, configs.insert_url);
            //common.setSelect();
            /*
            getProductName(function(product_name){
                $("#first_empty_line td [name='data\[product_name_new\]\[\]']").append(product_name);
            });
            */
            $('#J_List_Table_Add tbody').each(function () {

            });
            $("body").on("change","[name='data\[product_name_new\]\[\]']",function(){
                var type_id = $(this).find("option:selected").data('type-id');
                var product_id = $(this).val();
                var product_text = $(this).find("option:selected").text();
                var $tr = $(this).parents("tr");
                var $fac = $tr.find("[name='data\[factory_name_new\]\[\]']");
                var $brand = $tr.find("[name='data\[brand_name_new\]\[\]']");
                var $hidden = $tr.find("[name='data\[product_name_new_text\]\[\]']");
                $hidden.val(product_text);
                $fac.empty();
                $fac.prepend("<option value=''>选择厂家</option>");
                $brand.empty();
                $brand.prepend("<option value=''>选择牌号</option>");
                getFactoryName(type_id,product_id,function(factory_name){
                    $fac.append(factory_name)
                });
            });
            $("body").on("change","[name='data\[factory_name_new\]\[\]']",function(){
                var type_id = $(this).find("option:selected").data('type-id');
                var product_id = $(this).parents("tr").find("[name='data\[product_name_new\]\[\]']").val();
                var factory_id = $(this).val();
                var factory_text = $(this).find("option:selected").text();
                var $tr = $(this).parents("tr");
                var $brand = $tr.find("[name='data\[brand_name_new\]\[\]']");
                var $hidden = $tr.find("[name='data\[factory_name_new_text\]\[\]']");
                $hidden.val(factory_text);
                $brand.empty();
                $brand.prepend("<option value=''>选择牌号</option>");
                getBrandName(type_id,product_id,factory_id,function(brand_name){
                    $brand.append(brand_name)
                });
            });
            $("body").on("change","[name='data\[brand_name_new\]\[\]']",function(){
                var brand_text = $(this).find("option:selected").text();
                var $tr = $(this).parents("tr");
                var $hidden = $tr.find("[name='data\[brand_name_new_text\]\[\]']");
                $hidden.val(brand_text);
            });
            /*$("#first_empty_line td [name='data\[product_name_new\]\[\]']").on("change",function(){
                var type_id = $(this).find("option:selected").data('type-id');
                var product_id = $(this).val();
                var product_text = $(this).find("option:selected").text();
                var $tr = $(this).parents("tr");
                var $fac = $tr.find("[name='data\[factory_name_new\]\[\]']");
                var $brand = $tr.find("[name='data\[brand_name_new\]\[\]']");
                var $hidden = $tr.find("[name='data\[product_name_new_text\]\[\]']");
                $hidden.val(product_text);
                $fac.empty();
                $fac.prepend("<option value=''>选择厂家</option>");
                $brand.empty();
                $brand.prepend("<option value=''>选择牌号</option>");
                getFactoryName(type_id,product_id,function(factory_name){
                    $fac.append(factory_name)
                });
            });
            $("#first_empty_line td [name='data\[factory_name_new\]\[\]']").on("change",function(){
                var product_id = $(this).parents("tr").find("[name='data\[product_name_new\]\[\]']").val();
                var type_id = $(this).find("option:selected").data('type-id');
                var factory_id = $(this).val();
                var factory_text = $(this).find("option:selected").text();
                var $tr = $(this).parents("tr");
                var $brand = $tr.find("[name='data\[brand_name_new\]\[\]']");
                var $hidden = $tr.find("[name='data\[factory_name_new_text\]\[\]']");
                $hidden.val(factory_text);
                $brand.empty();
                $brand.prepend("<option value=''>选择牌号</option>");
                getBrandName(type_id,product_id,factory_id,function(brand_name){
                    $brand.append(brand_name)
                });
            });
            $("#first_empty_line td [name='data\[brand_name_new\]\[\]']").on("change",function(){
                var brand_text = $(this).find("option:selected").text();
                var $tr = $(this).parents("tr");
                var $hidden = $tr.find("[name='data\[brand_name_new_text\]\[\]']");
                $hidden.val(brand_text);
            });*/
            $('body').on('click','.delete_self',function () {
                $(this).parents(".empty_line").remove();
            });

            $('body').on('click','.add_one_line',function () {
                var html = template("empty_line-template");

                /*
                var htmltemp =
                    "<tr class='empty_line'><td>"+
                    "<select name='data[product_name_new][]' class='form-control input-sm search-options' required " +
                    "data-value='"+configs.sel_val+"'>"+
                    "<option value=''>选择品名</option>"+
                    "<option value='5' data-type-id='1'>PVC</option>"+
                    "<option value='6' data-type-id='1'>PP</option>"+
                    "<option value='56' data-type-id='1'>PET</option>"+
                    "<option value='4' data-type-id='1'>LLDPE</option>"+
                    "<option value='1' data-type-id='1'>ABS</option>"+
                    "<option value='7' data-type-id='1'>HIPS</option>"+
                    "<option value='3' data-type-id='1'>HDPE</option>"+
                    "<option value='9' data-type-id='2'>PC</option>"+
                    "<option value='2' data-type-id='1'>GPPS</option>"+
                    "<input type='hidden' name='data[product_name_new_text][]' value=''>"+
                    "</select></td>"+
                    "<td><select name='data[factory_name_new][]' class='form-control input-sm search-options' required>"+
                    "<option value=''>选择厂家</option>"+
                    "<input type='hidden' name='data[factory_name_new_text][]' value=''>"+
                    "</select></td>"+
                    "<td><select name='data[brand_name_new][]' class='form-control input-sm search-options' required>"+
                    "<option value=''>选择牌号</option>"+
                    "<input type='hidden' name='data[brand_name_new_text][]' value=''>"+
                    "</select></td>"+
                    "<td><!--<input type='text'  dtype='value' min-value='0.01' max-value='9999999.99' name='data[yesterday_new][]' maxlength='11'>--></td>"+
                    "<td><input type='text'  dtype='value' min-value='0.01' max-value='9999999.99' name='data[today_new][]' maxlength='11' required></td>"+
                    "<td><a href='javascript:;' class='btn btn-danger btn-xs delete_self'><i class='glyphicon glyphicon-trash'></i> 删除</a></td></tr>";
                    */
				$('.add_temp_table').append(html);

				$(".select2").select2();
                //tr.appendTo(".add_temp_table");
                //common.setSelect();
				/*
                getProductName(function(product_name){
                    $(".add_temp_table tr td [name='data\[product_name_new\]\[\]']").append(product_name);
                });
                */


            });
            function getProductName(callback){
                $.getJSON('/brand/brand/name', {"type_id":1,"name":""}, function (item) {
                    if( item.code=='000' ) {
                        var data =item.data;
                        var strOpt = "";
                        for(var i=0,len=data.length;i<len;i++){
                            strOpt += "<option value='"+data[i].id+"'>"+data[i].name+"</option>";
                        }
                        //console.log(strOpt);
                        callback(strOpt);
                    }
                });
            }
            function getFactoryName(type_id,product_id,callback){
                $.getJSON('/brand/brand/factory', {"type_id":type_id,"product_id":product_id}, function (item) {
                    if( item.code=='000' ) {
                        var data =item.data;
                        var strOpt = "";
                        for(var i=0,len=data.length;i<len;i++){
                            strOpt += "<option value='"+data[i].id+"' data-type-id='"+type_id+"'>"+data[i].name+"</option>";
                        }
                        //console.log(strOpt);
                        callback(strOpt);
                    }
                });
            }
            function getBrandName(type_id,product_id,factory_id,callback){
                $.getJSON('/brand/brand/brand', {"type_id":type_id,"product_id":product_id,"factory_id":factory_id}, function (item) {
                    if( item.code=='000' ) {
                        var data =item.data;
                        var strOpt = "";
                        for(var i=0,len=data.length;i<len;i++){
                            strOpt += "<option value='"+data[i].id+"' data-type-id='"+type_id+"'>"+data[i].name+"</option>";
                        }
                        //console.log(strOpt);
                        callback(strOpt);
                    }
                });
            }

        });


	}

});
